<template>
	<b-layout-index>
		<navbar bgcolor="#fff" :title="$t('二维码')"></navbar>
		<view class=" bg-white text-center pd-bottom-50">
			<image class="qrcode" mode="widthFix" :src="src"></image>

			<view>
				<view style="padding: 20px;" class="btns">
					<u-button @click="saveImageToPhotosAlbum" icon="photo-fill" class="mg-10" customStyle="{width:'80px'}" plain type="primary" shape="circle"
						:text="$t('保存图片')"></u-button>
					<u-button openType="share" icon="share-fill" class="mg-10" customStyle="{width:'80px'}" plain type="warning" shape="circle"
						:text="$t('好友转发')"></u-button>
				</view>

			</view>
		</view>
	</b-layout-index>
</template>

<script>
	import {
		pathToBase64,
		base64ToPath
	} from 'image-tools'
	import {
		getQrcode
	} from "../../../../api/home.js"
	export default {
		data() {
			return {
				src: "",
				topGrid: [{
					name: "aa"
				}, {
					name: "aa"
				}],
				ptype:2
	
			}
		},
		onLoad: function({ptype=2}) {
			this.ptype=ptype;
			//console.log(options)
			this.getQrcodeData()
		},
		methods: {
			getQrcodeData: function() {
				var userinfo=uni.getStorageSync("userInfo")
				var key = "typeimagessssmyqr"+uni.getStorageSync("lang")+"type"+this.ptype+"l"+userinfo.invite;
				var src = uni.getStorageSync(key);
				if (src) {
					this.src = "data:image/bmp;base64," + src;
				} else {
					getQrcode(this.ptype).then(res => {
	
						uni.setStorageSync(key, res)
						this.src = "data:image/bmp;base64," + res;
					})
				}
			},
			saveImageToPhotosAlbum() {
				var imgSrc = this.src //base64编码
				var number = Math.random()
				base64ToPath(imgSrc)
					.then(path => {
						console.log(path)
						uni.saveImageToPhotosAlbum({
							filePath: path,
							success: function(res) {
								uni.showToast({
									title: this.$t('保存成功'),
								})
							},
							fail: function(err) {
								console.log(err)
							}
						})
					})
					.catch(error => {
						console.error(error)
					})
				/* 		uni.saveImageToPhotosAlbum({
							filePath: imgSrc,
							success: function(res) {
								uni.showToast({
									title: '保存成功',
								})
							},
							fail: function(err) {
								console.log(err)
							}
						}) */
	
			}
	
	
		}
	
	}
</script>

<style>
	.btns {
		display: flex;
		justify-content: space-between;
		gap: 10px;
	}
	.qrcode{
		width: 100%;
		min-height:300px ;
		background-color: aliceblue;
	}
</style>
